﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using Infragistics.Win.UltraWinTree;

using YY.TreeTime;

namespace Ruler
{
    public partial class TreeTimer : UserControl
    {
        public TreeTimer()
        {
            InitializeComponent();

            this.treeList.Override.ItemHeight = _ItemHeight;
            this.panel1.BackColor = this.rulerControl1.BackColor;
        }

        
        private int _ItemHeight = 30;
        private Color _MainRegionBkColor = Color.Blue;
        private int _OneSliderRegionHeight = 10;
        private int _TotalHours = 3;

        private Color _ThumbColor = Color.Red;
        private Color _ThumbColor2 = Color.White;
        private Color _LineColor = Color.LightYellow;
        private float _LineHeight = 1;

        private ArrayList _SlideList = new ArrayList();

        public Color TreeBKColor
        {
            get { return this.treeList.Appearance.BackColor; }
            set { this.treeList.Appearance.BackColor = value; }
        
        }

        public int ItemHeight
        {
            get { return _ItemHeight; }
            set
            {
                _ItemHeight = value;
                this.treeList.Override.ItemHeight = _ItemHeight;
                SliderInfo oInfo = null;

                for (int i = 0; i < _SlideList.Count; i++)
                {
                    oInfo = (SliderInfo)_SlideList[i];
                    oInfo.Slider.Height = _ItemHeight;
                }
            }
        }

        public int TotalHours
        {
            get{return _TotalHours;}
            set
            {
                _TotalHours = value;
                this.rulerControl1.EndTime = value;

                SliderInfo oInfo = null;
                for (int i = 0; i < _SlideList.Count; i++)
                {
                    oInfo = (SliderInfo)_SlideList[i];
                    oInfo.Slider.Maximum = _TotalHours * 60;
                }

                Invalidate(true);
            }
        }

        public UltraTreeNode GetNode(string key)
        {
            return this.treeList.GetNodeByKey(key);
        }

        public delegate void MouseOnThumbHandler(RulerSlider slider, MouseEventArgs e);

        public event MouseOnThumbHandler MouseDownOnThumb;

        public Color LeftTopRegionBackColor
        {
            get { return this.panel1.BackColor; }
            set { this.panel1.BackColor = value; }
        }

        public Image LeftTopRegionBackImage
        {
            get { return this.panel1.BackgroundImage; }
            set { this.panel1.BackgroundImage = value; }
        }

        [Description("滑块的颜色")]
        [Category("TreeTime")]
        [DefaultValue(typeof(Color), "Red")]
        public Color ThumbColor
        {
            get { return _ThumbColor; }
            set
            {
                _ThumbColor = value;
                for (int i = 0; i < _SlideList.Count; i++)
                {
                    ((SliderInfo)_SlideList[i]).Slider.ThumbColor = _ThumbColor;
                }
            }
        }

        [Description("滑块的颜色2")]
        [Category("TreeTime")]
        [DefaultValue(typeof(Color), "White")]
        public Color ThumbColor2
        {
            get { return _ThumbColor2; }
            set
            {
                _ThumbColor2 = value;
                for (int i = 0; i < _SlideList.Count; i++)
                {
                    ((SliderInfo)_SlideList[i]).Slider.ThumbColor2 = _ThumbColor2;
                }
            }
        }

        [Description("滑动显示区域背景颜色")]
        [Category("TreeTime")]
        [DefaultValue(typeof(Color), "Blue")]
        public Color TreeTimeLineRegionBackColor
        {
            get { return _MainRegionBkColor; }
            set
            {
                _MainRegionBkColor = value;
                ChangeMainRegionBKColor(value);
            }
        }

        [Description("滑动线的颜色")]
        [Category("TreeTime")]
        [DefaultValue(typeof(Color), "LightYellow")]
        public Color LineColor
        {
            get { return _LineColor; }
            set
            {
                _LineColor = value;
                for (int i = 0; i < _SlideList.Count; i++)
                {
                    ((SliderInfo)_SlideList[i]).Slider.LineColor = _LineColor;
                }
            }
        }

        [Description("滑动线的粗度")]
        [Category("TreeTime")]
        [DefaultValue(typeof(float), "1")]
        public float LineHeight
        {
            get { return _LineHeight; }
            set
            {
                _LineHeight = value;
                for (int i = 0; i < _SlideList.Count; i++)
                {
                    ((SliderInfo)_SlideList[i]).Slider.LineHeight = _LineHeight;
                }
            }
        }

        [Description("RulerControl对象")]
        [Category("TreeTime")]
        public RulerControl Ruler
        {
            get { return this.rulerControl1; }
        }

        public void TreeTimer_MouseDownOnThumb(object sender, MouseEventArgs e)
        {
            MouseDownOnThumb((RulerSlider)sender, e);
        }
        
        private void ChangeMainRegionBKColor(Color color)
        {
            //SliderInfo oInfo = null;
            //for (int i = 0; i < _SlideList.Count; i++)
            //{
            //    oInfo = (SliderInfo)_SlideList[i];
            //    oInfo.Slider.BackColor = color;
            //}

            this.splitContainer1.Panel2.BackColor = color;
        }

        public void HiddenOneSlider(string sliderKey)
        {
            SliderInfo oInfo = null;
            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                if (oInfo.Key == sliderKey)
                {
                    oInfo.Slider.Visible = false;
                    break;
                }
            }
        }

        public void ShowOneSlider(string sliderKey)
        {
            SliderInfo oInfo = null;
            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                if (oInfo.Key == sliderKey)
                {
                    oInfo.Slider.Visible = true;
                    break;
                } 
            }
        }

        [Description("一个滑块显示区域的高度")]
        [Category("TreeTime")]
        [DefaultValue(10)]
        public int OneSliderRegionHeight
        {
            get { return _OneSliderRegionHeight; }
            set
            {
                _OneSliderRegionHeight = value;

                SliderInfo oInfo = null;
                for (int i = 0; i < _SlideList.Count; i++)
                {
                    oInfo = (SliderInfo)_SlideList[i];
                    oInfo.Slider.Height = _OneSliderRegionHeight;
                }
            }
        }

        /// <summary>
        /// 在根节点下的自己节点集合的最后，增加一个节点
        /// </summary>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <param name="curtime"></param>
        public void AddNode(string key, string name, int curtime)
        {

            UltraTreeNode oNode = this.treeList.GetNodeByKey(key);

            if (oNode != null)
            {
                throw new Exception("已经存在相同关键字的对象");
            }

            oNode = new UltraTreeNode();
            oNode.Key = key;
            oNode.Text = name;
            oNode.LeftImages.Add(this.imgList.Images[0]);
            this.treeList.Nodes.Add(oNode);
            
            RulerSlider oSlider = AddSlider(key, name, curtime);

            oNode.Tag = oSlider;

            oNode.BringIntoView();
        }

        /// <summary>
        /// 添加一个同级节点
        /// </summary>
        /// <param name="oParentNode"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <param name="curtime"></param>
        public void AddSonNode(UltraTreeNode oParentNode , string key,string name, int curtime)
        {
            UltraTreeNode oNode = this.treeList.GetNodeByKey(key);
            if (oNode != null) {throw new Exception("已经存在相同关键字的对象");}

            oNode = new UltraTreeNode();
            oNode.Key = key;
            oNode.Text = name;
            oNode.LeftImages.Add(this.imgList.Images[0]);

            RulerSlider oInSlider = null;
            
            oInSlider =(RulerSlider)this.GetLastNestGarNode(oParentNode).Tag;
            oInSlider = InsertAfterNode(oInSlider.Key, key, name, curtime);
            oNode.Tag = oInSlider;

            oParentNode.Nodes.Add(oNode);

            oNode.BringIntoView();
        }

        /// <summary>
        /// 插入一个树状显示节点
        /// </summary>
        /// <param name="beforeNode"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <param name="curtime"></param>
        public void InsertBeforeNode(UltraTreeNode beforeNode, string key, string name, int curtime)
        {
            UltraTreeNode oNode = this.treeList.GetNodeByKey(key);
            if (oNode != null) { throw new Exception("已经存在相同关键字的对象"); }

            oNode = new UltraTreeNode();
            oNode.Key = key;
            oNode.Text = name;
            oNode.LeftImages.Add(this.imgList.Images[0]);
            if (beforeNode.Parent == null)
            {
                this.treeList.Nodes.Insert(beforeNode.Index, oNode);
            }
            else
            {
                beforeNode.Parent.Nodes.Insert(beforeNode.Index, oNode);
            }

            RulerSlider oInSlider = null;
            oInSlider = (RulerSlider)beforeNode.Tag;

            oInSlider = InsertBeforeNode(oInSlider.Key, key, name, curtime);

            oNode.Tag = oInSlider;
            oNode.BringIntoView();
        }

        private UltraTreeNode GetLastNestGarNode(UltraTreeNode node)
        {
            if (node.Nodes.Count <= 0) { return node; }
            else
            {
                return node.Nodes[node.Nodes.Count - 1];
            }
        }

        private RulerSlider AddSlider(string key, string name, int curtime)
        {
            int iTop = this.rulerControl1.Height;
            SliderInfo oInfo=null;


            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo=(SliderInfo)_SlideList[i];
                iTop += oInfo.Slider.Height;
                if (key == oInfo.Key)
                {
                    throw new Exception("SliderList 已经存在 相同关键字的对象");
                }
            }

            oInfo = new SliderInfo();
            oInfo.Key = key;
            RulerSlider sl = new RulerSlider();
            this.splitContainer1.Panel2.SuspendLayout();

            sl.Location = new Point(0, iTop);
            sl.Maximum = _TotalHours * 60;
            sl.Height = GetNodeRegionHeight;
            sl.Value = curtime;
            //sl.Visible = true;
            sl.Key = key;
            sl.Width = this.splitContainer1.Panel2.Width;
            this.splitContainer1.Panel2.Controls.Add(sl);
            
            sl.Dock = DockStyle.Top;
            sl.BringToFront();
            this.splitContainer1.Panel2.ResumeLayout();
          
            oInfo.Slider = sl;
            sl.MouseDownOnThumb += new MouseEventHandler(TreeTimer_MouseDownOnThumb);
            RulerSilderInit(sl);
            _SlideList.Add(oInfo);
            return sl;
        }

        public void DeleteNode(string key)
        {
            SliderInfo oInfo = null;

            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                if (key == oInfo.Key)
                {
                    _SlideList.RemoveAt(i);
                    oInfo.Slider.Parent.Controls.Remove(oInfo.Slider);
                    oInfo.Slider.Dispose();
                }
            }
        }

        public void Hidden(string key)
        {
            SliderInfo oInfo = null;

            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                if (key == oInfo.Key)
                {
                    oInfo.Slider.Visible = false;
                }
            }
        }

        public void Show(string key)
        {
            SliderInfo oInfo = null;

            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                 if (key == oInfo.Key)
                {
                    oInfo.Slider.Visible=true;
                }
            }
        }

        private RulerSlider InsertBeforeNode(string insertNodekey,string key, string name, int curtime)
        {
            int iTop = this.rulerControl1.Height;
            SliderInfo oInfo = null;
            RulerSlider sl=null;

            bool bInserted = false;

            this.splitContainer1.Panel2.SuspendLayout();

            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                iTop += oInfo.Slider.Height;
                if (bInserted)
                {
                    oInfo.Slider.Top += GetNodeRegionHeight;
                }
                if (insertNodekey == oInfo.Key)
                {
                    sl = new RulerSlider();
                    sl.Top = oInfo.Slider.Top;
                    sl.Key = key;
                    sl.Dock = DockStyle.Top;
                    sl.Maximum = _TotalHours * 60;
                    sl.Height = GetNodeRegionHeight;
                    sl.Value = curtime;

                    oInfo.Slider.Top += GetNodeRegionHeight;

                    oInfo = new SliderInfo();
                    oInfo.Key = key;
                    oInfo.Slider = sl;

                    this.splitContainer1.Panel2.Controls.Add(sl);
                    sl.MouseDownOnThumb += new MouseEventHandler(TreeTimer_MouseDownOnThumb);
                    RulerSilderInit(sl);
                    _SlideList.Insert(i, oInfo);
                    break;
                }
            }
            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                oInfo.Slider.BringToFront();
            }

            this.splitContainer1.Panel2.ResumeLayout();

            return sl;
        }

        private RulerSlider InsertAfterNode(string insertNodekey, string key, string name, int curtime)
        {
            int iTop = this.rulerControl1.Height;
            SliderInfo oInfo = null;
            RulerSlider sl = null;

            bool bInserted = false;

            this.splitContainer1.Panel2.SuspendLayout();

            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                iTop += oInfo.Slider.Height;
                if (bInserted)
                {
                    oInfo.Slider.Top += GetNodeRegionHeight;
                }
                if (insertNodekey == oInfo.Key)
                {
                    oInfo.Slider.Top += GetNodeRegionHeight;
                    sl = new RulerSlider();
                    sl.Top = oInfo.Slider.Top;
                    sl.Key = key;
                    sl.Dock = DockStyle.Top;
                    sl.Maximum = _TotalHours * 60;
                    sl.Height = GetNodeRegionHeight;
                    sl.Value = curtime;

                    oInfo = new SliderInfo();
                    oInfo.Key = key;
                    oInfo.Slider = sl;

                    this.splitContainer1.Panel2.Controls.Add(sl);
                    sl.MouseDownOnThumb += new MouseEventHandler(TreeTimer_MouseDownOnThumb);
                    RulerSilderInit(sl);
                    _SlideList.Insert(i+1, oInfo);
                    break;
                }
            }
            for (int i = 0; i < _SlideList.Count; i++)
            {
                oInfo = (SliderInfo)_SlideList[i];
                oInfo.Slider.BringToFront();
            }

            this.splitContainer1.Panel2.ResumeLayout();

            return sl;
        }
            
        /// <summary>
        /// 获取树形节点的高度
        /// </summary>
        private int GetNodeRegionHeight
        {
            get
            {
                return this.treeList.Override.ItemHeight;
            }
        }

        private void treeList_AfterExpand(object sender, Infragistics.Win.UltraWinTree.NodeEventArgs e)
        {
            if (e.TreeNode == null) { return; }
            for (int i = 0; i < e.TreeNode.Nodes.Count; i++)
            {
                ShowSons(e.TreeNode.Nodes[i]);
            }
        }

        private void treeList_AfterCollapse(object sender, Infragistics.Win.UltraWinTree.NodeEventArgs e)
        {
            if (e.TreeNode == null) { return; }

            for (int i = 0; i < e.TreeNode.Nodes.Count; i++)
            {
                HiddenSons(e.TreeNode.Nodes[i]);
            }
        }

        private void HiddenSons(UltraTreeNode oNode)
        {
            for (int i = 0; i < oNode.Nodes.Count; i++)
            {
                HiddenSons(oNode.Nodes[i]);
            }

            ((RulerSlider)oNode.Tag).Visible =false; 
        }

        private void ShowSons(UltraTreeNode oNode)
        {
            for (int i = 0; i < oNode.Nodes.Count; i++)
            {
                if (oNode.Nodes[i].Expanded)
                {
                    ShowSons(oNode.Nodes[i]);
                }
            }

            ((RulerSlider)oNode.Tag).Visible = true;
        }

        private void splitContainer1_Panel2_Scroll(object sender, ScrollEventArgs e)
        {
            if(e.ScrollOrientation==ScrollOrientation.VerticalScroll)
            {
                
            }
        }

        private void TreeTimer_Resize(object sender, EventArgs e)
        {
           
        }

        private void RulerSilderInit(RulerSlider slider)
        {
            slider.ThumbColor = _ThumbColor;
            slider.ThumbColor2 = _ThumbColor2;
            slider.LineColor = _LineColor;
            slider.LineHeight = _LineHeight;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);


        }
    }

    public class SliderInfo
    {
        public SliderInfo() { }

        private string _Key = string.Empty;
        private RulerSlider _RulerSlider = null;

        public string Key
        {
            get { return _Key; }
            set
            {
                _Key = value;

                if (_RulerSlider != null)
                {
                    _RulerSlider.Key = _Key;
                }
            }
        }


        public RulerSlider Slider
        {
            get { return _RulerSlider; }
            set { _RulerSlider = value; }
        }
    }
}
